/**
 * @Title: warning_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @date 2014年10月27日 下午4:43:38
 * @version V1.0
 */
;
(function(){
    'use strict';

    var app = angular.module('fwa.mainApp');
    app.controller('OrgReviewUpdateEvaluationCtrl', ['$scope','CommunityService','UpdateEvaluationService','OrgApproveEvaluationService','$http','$modal','$state','BaseURL','Modal','FwaTabsetService','$stateParams',
        function($scope,CommunityService,UpdateEvaluationService,OrgApproveEvaluationService,$http,$modal,$state,baseUrl,Modal,fwaTabsetService,$stateParams){

    		//页面
    		$scope.includeUrl = 'views/hcpms/evaluation/evaluationOrg/orgReview/orgReviewCheckTableEvaluation.html';

            $scope.communityService = CommunityService;
            CommunityService.initCodeListDatas();

            $scope.intgrityEvalTypeInfo={};
            //保存tabclick的 点击对象
     	    $scope.tabClickData={};
            //tab切换事件
            $scope.tabClick = function(data) {
                $scope.intgrityEvalTypeInfo=data;
                $scope.selectedTab = data;
                loadIntgrityEvalDetailInfo(data.evaluRuleId,data.intgrityEvalChildId);
                // 根据诚信考评表子表 的主建去加载对应的 审核意见
                if(data.intgrityEvalChildId) {
	               	 $scope.tabClickData=data;
	               	 loadEvaluationOpinionInfo(data.intgrityEvalChildId);
                }
             }
            
            /**
             * 根据诚信考评字表的id加载对应的审核意见
             */
           
            function loadEvaluationOpinionInfo(evalChildId) {
           	 	var parameter = {
           	 						evalChildId:evalChildId
           			 			};
	           	 OrgApproveEvaluationService.loadEvaluationOpinionInfo(parameter).success(function(response){
	                if(response.flag){
	               	   //审核的意见
	               	  if(response.auditData){
	               		  var opinion=response.auditData.opinion;
	               		  var opinionDesc=response.auditData.opinionDesc;
	                   		 $scope.intgrityEvalTypeInfo.opinion=opinion;
	                   		 $scope.intgrityEvalTypeInfo.opinionDesc=opinionDesc;
	               	  }
	               }
	            });
           }
            //初始化请求参数
            var evaluationCode = null;
            if ($stateParams.evaluationCode) {
            	evaluationCode = $stateParams.evaluationCode;
            }
            /**
             * 初始化诚信单位考评详情
             */
            $scope.intgrityEvalData = {};
            function loadIntgrityEvalData() {
                var parameter={evaluationCode:evaluationCode};
                OrgApproveEvaluationService.loadIntgrityEvalInfo(parameter).success(function(response){
                   if(response.flag){
                       $scope.intgrityEvalData = response.data;
                  }
               });
            }
            /**
             * 初始化诚信考评类别详情
             */
            $scope.tabs = [];
            function loadIntgrityEvalTypeInfo() {
                var parameter = {
                		evaluationCode:evaluationCode
                		};
                OrgApproveEvaluationService.loadIntgrityEvalTypeInfo(parameter).success(function(response){
                    if(response.flag){
                    	$scope.tabs = response.list;
                    	 //第一次默认选中第一个tab页
                        $scope.tabClick($scope.tabs[0]);
                    }
                });
            }
            
            var intgrityEvalMap = {};
            /**
             * 初始化诚信考评详情
             */
            $scope.intgrityEvalInfo = {};
            function loadIntgrityEvalDetailInfo(evaluRuleId,intgrityEvalChildId) {
                if (!intgrityEvalMap[evaluRuleId]) {
                	 $scope.loading=true;
                	 var parameter= {evaluRuleId:evaluRuleId,intgrityEvalChildId:intgrityEvalChildId};
                    OrgApproveEvaluationService.loadIntgrityEvalDetailInfo(parameter).success(function(response){
                    	$scope.loading=false;
                    	if (response.flag) {
                            intgrityEvalMap[evaluRuleId] = response.result;
                            getTableDatas(intgrityEvalMap[evaluRuleId]);
                        }
                    });
                }
                else {
                	$scope.loading=false;
                	$scope.intgrityEvalInfo = intgrityEvalMap[evaluRuleId];
                	getTableDatas(intgrityEvalMap[evaluRuleId]);
                }
                
            }

            //json 数据重新组合
            function getTableDatas(datas) {
                if (datas) {
                    var tableDatas = [];
                    angular.forEach(datas,function(oneData,i) {
                        var oneLength = 0;
                        oneData.no = i + 1;
                        tableDatas.push(oneData);
                        angular.forEach(oneData.list,function(twoData,j) {
                            twoData.rowSpan = twoData.list.length;
                            if (j == 0) {
                                oneData.data = twoData;
                            }
                            else {
                                twoData.oneHide = true;
                                tableDatas.push(twoData);
                            }
                            if (twoData.list && twoData.list.length > 0 ) {
                                angular.forEach(twoData.list,function(data,k) {
                                    oneLength++;
                                    if (data.isConfirm == '0') {
                                        data.checked = true;
                                    }
                                    else {
                                        data.checked = true;
                                    }
                                    if (k == 0) {
                                        twoData.data = data;
                                    }
                                    else {
                                        data.oneHide = true;
                                        data.twoHide = true;
                                        tableDatas.push(data);
                                    }
                                });
                            }
                            else {
                                oneLength++;
                            }
                        });
                        oneData.rowSpan = oneLength;
                    });
                    $scope.tableData = tableDatas;
                }
            }

            /**
             * 返回
             */
            $scope.goBack = function() {
                $state.go("evaluationOrgReviewManager.evaluationOrgReviewList");
            };

            /**
             * 保存
             */
            $scope.submitForEvaluation = function() {
                if(check()){
                    $scope.intgrityEvalTypeInfo.evaluationCode=evaluationCode;
                    var parameter= $scope.intgrityEvalTypeInfo;
                      $scope.loading=true;
                    OrgApproveEvaluationService.saveEvaluationOpinion(parameter).success(function(response){
                    	 $scope.loading=false;
                    	  if(response){
                    		  
                    		  if(response.flag && response.success){
                    			  $scope.goBack();
                                  Modal.alert(response.msg);
                                  return;
                    		  }else if(response.flag){
                    			  Modal.alert(response.msg);
                                  return;
                    		  }else{
                             	 Modal.alert(response.msg);
                              }
                    		  
                    	  }
                    })
                }
            }
           function check(){
               if(!$scope.intgrityEvalTypeInfo.opinion){
                   Modal.alert('审核意见为必填项！');
                   return false;
               }
               if(!$scope.intgrityEvalTypeInfo.opinionDesc){
                   Modal.alert('意见说明为必填项！');
                   return false;
               }
               return true;
           }

            loadIntgrityEvalData();
            loadIntgrityEvalTypeInfo();
            
        }]);
})();
